package com.example.pcl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pcl.exception.ObjectException;
import com.example.pcl.pojo.dto.KnowledgeQueryDto;
import com.example.pcl.pojo.entity.MentalHealthKnowledge;
import com.example.pcl.mapper.MentalHealthKnowledgeMapper;
import com.example.pcl.result.PageResult;
import com.example.pcl.service.IMentalHealthKnowledgeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 心理健康知识表，存储与心理健康相关的知识内容 服务实现类
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@Service
public class MentalHealthKnowledgeServiceImpl extends ServiceImpl<MentalHealthKnowledgeMapper, MentalHealthKnowledge> implements IMentalHealthKnowledgeService {
    @Override
    public void addKnowledge(MentalHealthKnowledge knowledge) {
        boolean result = save(knowledge);
        if (!result) {
            throw new ObjectException("添加知识失败");
        }
    }

    @Override
    public void updateKnowledge(MentalHealthKnowledge knowledge) {
        boolean result = updateById(knowledge);
        if (!result) {
            throw new ObjectException("更新知识失败");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatchKnowledge(List<Integer> knowledgeIds) {
        if (CollectionUtil.isNotEmpty(knowledgeIds)) {
            boolean result = removeBatchByIds(knowledgeIds.stream().distinct().toList());
            if (!result) {
                throw new ObjectException("批量删除知识失败");
            }
        }
    }

    @Override
    public PageResult<?> listKnowledge(@NotNull KnowledgeQueryDto queryDto) {
        String category = queryDto.getCategory();
        Integer type = queryDto.getType();
        String title = queryDto.getTitle();

        Page<MentalHealthKnowledge> knowledgeList = page(new Page<>(queryDto.getPageNum(), queryDto.getPageSize()), new LambdaUpdateWrapper<MentalHealthKnowledge>()
                .like(StringUtils.isNotEmpty(category), MentalHealthKnowledge::getCategory, category)
                .like(StringUtils.isNotEmpty(title), MentalHealthKnowledge::getTitle, title)
                .eq(ObjectUtil.isNotNull(type), MentalHealthKnowledge::getType, type)
                .orderByDesc(MentalHealthKnowledge::getCreatedAt));
        return new PageResult<>(knowledgeList.getTotal(), knowledgeList.getRecords());
    }

    @Override
    public MentalHealthKnowledge getKnowledgeById(Integer knowledgeId) {
        MentalHealthKnowledge knowledge = getById(knowledgeId);
        if (knowledge == null) {
            throw new ObjectException("未找到知识");
        }
        return knowledge;
    }
}
